package com.fowo.api.datainterface.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fowo.api.datainterface.constants.RequestMethodKind;
import com.fowo.api.datainterface.util.k3cloud.domain.*;
import com.fowo.api.datainterface.util.k3cloud.service.BillQueryService;
import com.fowo.api.datainterface.util.k3cloud.service.BillSaveService;
import com.fowo.api.datainterface.util.k3cloud.service.BillSubmitService;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 简化k3cloud操作
 *
 * @author dong.tang
 */
@Component
@Slf4j
public class RestUtilK3Cloud {

    @Resource
    private BillQueryService billQueryService;
    @Resource
    private BillSaveService billSaveService;
    @Resource
    private BillSubmitService billSubmitService;

    private void auth(String endPoint, Authentication auth) {
        if (StrUtil.isEmpty(endPoint) || ObjectUtil.isEmpty(auth)) {
            Assert.isTrue(false, "请设置设置授权信息！");
        }
        this.billQueryService.setEndPoint(endPoint);
        this.billQueryService.setAuth(auth);
        this.billSaveService.setEndPoint(endPoint);
        this.billSaveService.setAuth(auth);
        this.billSubmitService.setEndPoint(endPoint);
        this.billSubmitService.setAuth(auth);
    }

    private K3CloudApi client;
    public K3CloudApi getClient(IdentifyInfo auth) {
        return ObjectUtil.isNotEmpty(client) ? client : new K3CloudApi(auth);
    }

    public Object k3cloudrequest(String endPoint, Authentication auth, String formId, String method, Object params) {
        auth(endPoint, auth);
        if (ObjectUtil.isEmpty(formId)) {
            Assert.isTrue(false, StrUtil.concat(true, " formId不能为空"));
        }
        try {
            if (StrUtil.equalsIgnoreCase(RequestMethodKind.Query.getValue(), method)) //表单数据查询服务
            {
                List<BillQuery> list = new ArrayList<>();
                try {
                    BillQuery billQuery = JsonUtil.getJsonToBean(params, BillQuery.class);
                    billQuery.setFormId(formId);
                    list.add(billQuery);
                } catch (Exception e) {
                    Assert.isTrue(false, "参数配置错误");
                }
                return Convert.toStr(billQueryService.execute(list));
            } else if (StrUtil.equalsIgnoreCase(RequestMethodKind.Save.getValue(), method)) //保存表单数据服务
            {
                List<Object> p = new ArrayList<>(2);
                try {
                    BillSave billSave = new BillSave(formId);
                    billSave.setModel(Convert.toMap(String.class, Object.class, params));
                    p.add(formId);
                    p.add(billSave);
                } catch (Exception e) {
                    Assert.isTrue(false, "参数配置错误");
                }
                return Convert.toStr(billSaveService.execute(p));
            } else if (StrUtil.equalsIgnoreCase(RequestMethodKind.Submit.getValue(), method)) //提交表单数据服务
            {
                List<Object> p = new ArrayList<>(2);
                try {
                    BillSubmit billSubmit = JsonUtil.getJsonToBean(params, BillSubmit.class);
                    p.add(formId);
                    p.add(billSubmit);
                } catch (Exception e) {
                    Assert.isTrue(false, "参数配置错误");
                }
                return Convert.toStr(this.billSubmitService.execute(p));
            } else {
                Assert.isTrue(false, "服务暂未提供！");
            }
        } catch (Exception e) {
            Assert.isTrue(false, "HTTP调用错误 ，错误提示:" + e.getMessage());
        }
        return null;
    }


    public Object k3cloudrequest(IdentifyInfo auth, String formId, String method, String params) {
        if (ObjectUtil.isEmpty(formId)) {
            Assert.isTrue(false, StrUtil.concat(true, "formId不能为空"));
        }
        try {
            if (StrUtil.equalsIgnoreCase(RequestMethodKind.Query.getValue(), method)) //表单数据查询服务
            {
                return Convert.toStr(getClient(auth).executeBillQuery(params));
            } else if (StrUtil.equalsIgnoreCase(RequestMethodKind.Save.getValue(), method)) //保存表单数据服务
            {
                return Convert.toStr(getClient(auth).save(formId, params)) ;
            } else if (StrUtil.equalsIgnoreCase(RequestMethodKind.Submit.getValue(), method)) //提交表单数据服务
            {
                return  Convert.toStr(getClient(auth).submit(formId, params));
            } else {
                Assert.isTrue(false, "服务暂未提供！");
            }
        } catch (Exception e) {
            Assert.isTrue(false, "调用错误 ，错误提示:" + e.getMessage());
        }
        return null;
    }


}
